<template>
  <div>
    <el-row :gutter="20">
      <div class="app-container">
        <el-row class="relationContainer">
          <el-col :span="8"><span class="el-custom-title">工段管理</span></el-col>
          <el-col :span="16" style="float: right">
            <el-button-group style="float: right">
              <el-button
                type="primary"
                icon="el-icon-plus"
                class="el-button--mini"
                @click="handleCreate"
                >添加</el-button
              >
              <el-button type="primary"
               icon="el-icon-edit"
               size="mini" @click="handleUpdate()">
                编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                 icon="el-icon-delete"
                @click="ui_delete(null, null)"
              >
                删除
              </el-button>
            </el-button-group>
          </el-col>
        </el-row>
        <el-row type="flex" justify="start">
          <el-table
            v-loading="listLoading"
            :data="list"
            element-loading-text="Loading"
            border
            fit
            highlight-current-row
            @row-click="rowClick"
            @selection-change="handleSelectionChange"
          >
            >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column
              align="center"
              label="工段编号"
              width="95"
              style="display: none"
            >
              <template slot-scope="scope">
                {{ scope.row.workSectionNo }}
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="工段名称"
              width="95"
              style="display: none"
            >
              <template slot-scope="scope">
                {{ scope.row.workSectionName }}
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="备注"
              width="95"
              style="display: none"
            >
              <template slot-scope="scope">
                {{ scope.row.comment }}
              </template>
            </el-table-column>
          </el-table>
        </el-row>
            <el-row>
          <!-- <pagination
            background
            v-show="total > 0"
            :total="total"
            :page.sync="listQuery.page"
            :limit.sync="listQuery.limit"
            :page-sizes="listQuery.pageSizes"
            @pagination="fetchData"
          /> -->
           <el-pagination
        background
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.page"
        :page-size="listQuery.limit"
        :page-sizes="listQuery.pageSizes"
        :total="total">
      </el-pagination>
        </el-row>

      </div>
    </el-row>
    
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="120px"
        style="width: 400px; margin-left: 50px"
      >
        <el-form-item label="工段编号" prop="workSectionNo">
          <el-input v-model="temp.workSectionNo" />
        </el-form-item>
        <el-form-item label="工段名称" prop="workSectionName">
          <el-input v-model="temp.workSectionName" />
        </el-form-item>
        <el-form-item label="备注" prop="comment">
          <el-input v-model="temp.comment" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false"> 取消 </el-button>
        <el-button
          type="primary"
          @click="dialogStatus === 'create' ? ui_create() : ui_update()"
        >
          确定
        </el-button>
      </div>
    </el-dialog>
    <router-view></router-view>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import request from "@/utils/request";
import {
  getAll,
  getOne,
  getList,
  create,
  update,
  deleteBatch,
} from "@/api/mesWorkSection";

export default {
  components: { Pagination },
  data() {
    return {
      filterText: "",
      data2: [],
      defaultProps: {
        children: "children",
        label: "MESWorkSection",
      },
      list: null,
      list2: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 6,
        pageSizes: [6, 10, 14],
      },
      currentMESWorkSectionId: 0,
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "创建",
      },
      rules: {
        workSectionNo: [
          {
            required: true,
            message: "工段编号不能为空！",
            trigger: "change",
          },
        ],
        workSectionName: [
          {
            required: true,
            message: "工段名称不能为空！",
            trigger: "change",
          },
        ],
      },
      temp: {
        id: undefined,
        workSectionNo: "",
        workSectionName: "",
        comment: "",
      },
      //表格變量
      lstSelected: [],
    };
  },
  watch: {
    filterText(val) {
      this.$refs.tree2.filter(val);
    },
  },
  created() {
    this.fetchData();
  },
  methods: {
    handleSelectionChange(val) {
      this.lstSelected = val;
    },
    fetchData() {
      this.listLoading = true;
      let params = {
        name: "",
        page: this.listQuery.page,
        size: this.listQuery.limit,
      };
      getList(params).then((response) => {
        this.list = response.data.records;
        this.listLoading = false;
        this.total = response.data.total;
      });
    },

    rowClick(row, column, event) {
      this.currentTherbligsTypeId = row.id;
    },
    ui_create() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.temp.id = 0; //自增
          create(this.temp).then(() => {
            this.list.unshift(this.temp);
            this.dialogFormVisible = false;
            this.$notify({
              title: "Success",
              message: "添加成功！",
              type: "success",
              duration: 2000,
            });
          });
        }
      });
    },
    ui_update() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          update(this.temp).then(() => {
            const index = this.list.findIndex((v) => v.id === this.temp.id);
            this.list.splice(index, 1, this.temp);
            this.dialogFormVisible = false;
            this.$notify({
              title: "Success",
              message: "修改成功！",
              type: "success",
              duration: 2000,
            });
          });
        }
      });
    },
    ui_delete(row, index) {
      if (this.lstSelected == null || this.lstSelected.length === 0) {
        this.$message.error("请先选择要删除的工段");
        return;
      }
      let ids = [];
      this.lstSelected.map(function (elm) {
        ids.push(elm.id);
      });
      deleteBatch(ids).then(() => {
        ids.forEach((elm) => {
          const index = this.list.findIndex((v) => v.id === elm);
          this.list.splice(index, 1);
        });

        this.dialogFormVisible = false;
        this.$notify({
          title: "Success",
          message: "删除成功！",
          type: "success",
          duration: 2000,
        });
      });
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        workSectionNo: "",
        workSectionName: "",
        comment: "",
      };
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleUpdate() {
      if (this.lstSelected == null || this.lstSelected.length !== 1) {
        this.$message.error("请先选择一条工段");
        return;
      }
      this.temp = Object.assign({}, this.lstSelected[0]); // copy obj
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
         handleCurrentChange(val) {
        this.listQuery.page = val;
        this.fetchData();
      },
  },
};
</script>
<style>
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
